地理信息上传逻辑优化

chengzhenyu лет %!s(int64=8): %!d(string=назад)
Родитель
Сommit
4abc31b420

+ 0 - 0
app/keytool


+ 0 - 5
app/src/main/java/ai/pai/client/activity/MainActivity.java

@@ -98,11 +98,6 @@ public class MainActivity extends AppCompatActivity
98 98
     protected void onResume() {
99 99
         super.onResume();
100 100
         callGuideBtn.setVisibility(TextUtils.isEmpty(Preferences.getInstance(this).getTourGuidePhone()) ? View.INVISIBLE : View.VISIBLE);
101
-        if(Preferences.getInstance(this).isTourMode()){
102
-            Intent intent = new Intent(this, MyLocationService.class);
103
-            intent.putExtra("command",MyLocationService.COMMAND_START_LOCATION);
104
-            startService(intent);
105
-        }
106 101
     }
107 102
 
108 103
 

+ 9 - 0
app/src/main/java/ai/pai/client/db/Preferences.java

@@ -59,6 +59,15 @@ public class Preferences {
59 59
         return mPrefs.getLong("gatherTime",0);
60 60
     }
61 61
 
62
+
63
+    public void setTourEndTime(long tourEndTime){
64
+        mPrefs.edit().putLong("tourEndTime",tourEndTime).commit();
65
+    }
66
+
67
+    public long getTourEndTime(){
68
+        return mPrefs.getLong("tourEndTime",0);
69
+    }
70
+
62 71
     public boolean isTourMode(){
63 72
         return mPrefs.getBoolean("isTourMode",false);
64 73
     }

+ 5 - 3
app/src/main/java/ai/pai/client/fragments/TabRecentPhotoFragment.java

@@ -35,7 +35,6 @@ import java.util.HashMap;
35 35
 
36 36
 import ai.pai.client.R;
37 37
 import ai.pai.client.adapter.RecentPhotoStaggeredAdapter;
38
-import ai.pai.client.beans.BannerBean;
39 38
 import ai.pai.client.beans.GroupPhotoItem;
40 39
 import ai.pai.client.db.DBService;
41 40
 import ai.pai.client.db.Preferences;
@@ -67,7 +66,7 @@ public class TabRecentPhotoFragment extends BaseFragment implements SwipeRefresh
67 66
     @Override
68 67
     public void onCreate( Bundle savedInstanceState) {
69 68
         super.onCreate(savedInstanceState);
70
-        photoList = new ArrayList<GroupPhotoItem>();
69
+        photoList = new ArrayList<>();
71 70
         left = Integer.MAX_VALUE;
72 71
     }
73 72
 
@@ -83,7 +82,7 @@ public class TabRecentPhotoFragment extends BaseFragment implements SwipeRefresh
83 82
         StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
84 83
         recyclerView.setLayoutManager(layoutManager);
85 84
         if(Preferences.getInstance(getActivity()).isGuestLogin()){
86
-            photoList = new ArrayList<GroupPhotoItem>();
85
+            photoList = new ArrayList<>();
87 86
         }else{
88 87
             photoList = DBService.getInstance(getActivity()).getRecentPhotos();
89 88
         }
@@ -146,6 +145,8 @@ public class TabRecentPhotoFragment extends BaseFragment implements SwipeRefresh
146 145
                             Preferences.getInstance(context).setTourGuidePhone(phone);
147 146
                             String gatherTimeStr = group.getString("gather_at");
148 147
                             Preferences.getInstance(context).setGatherTime(TimeUtils.getStandardTimeInMiliSec(gatherTimeStr));
148
+                            String endTimeStr = group.getString("ended_at");
149
+                            Preferences.getInstance(context).setTourEndTime(TimeUtils.getStandardTimeInMiliSec(endTimeStr));
149 150
                             JSONObject banners = group.getJSONObject("banners");
150 151
                             images[0] = banners.getString("attentions");
151 152
                             images[1] = banners.getString("schedules");
@@ -153,6 +154,7 @@ public class TabRecentPhotoFragment extends BaseFragment implements SwipeRefresh
153 154
                             Preferences.getInstance(context).setTourMode(false);
154 155
                             Preferences.getInstance(context).setTourGuidePhone("");
155 156
                             Preferences.getInstance(context).setGatherTime(0);
157
+                            Preferences.getInstance(context).setTourEndTime(0);
156 158
                         }
157 159
                         return true;
158 160
                     }

+ 135 - 42
app/src/main/java/ai/pai/client/services/MyLocationService.java

@@ -1,9 +1,14 @@
1 1
 package ai.pai.client.services;
2 2
 
3 3
 import android.app.Service;
4
+import android.content.Context;
4 5
 import android.content.Intent;
6
+import android.os.AsyncTask;
5 7
 import android.os.Bundle;
8
+import android.os.Handler;
6 9
 import android.os.IBinder;
10
+import android.os.Message;
11
+import android.text.TextUtils;
7 12
 
8 13
 import com.amap.api.location.AMapLocation;
9 14
 import com.amap.api.location.AMapLocationClient;
@@ -11,29 +16,45 @@ import com.amap.api.location.AMapLocationClientOption;
11 16
 import com.amap.api.location.AMapLocationListener;
12 17
 import com.android.common.executors.ThreadExecutor;
13 18
 import com.android.common.utils.LogHelper;
19
+import com.android.common.utils.TimeUtils;
20
+
21
+import org.json.JSONObject;
14 22
 
15 23
 import java.util.HashMap;
16 24
 
17
-import ai.pai.client.BuildConfig;
18 25
 import ai.pai.client.db.Preferences;
19 26
 import ai.pai.client.utils.HttpPostTask;
20 27
 import ai.pai.client.utils.UrlContainer;
21 28
 
22 29
 
23
-public class MyLocationService extends Service {
30
+public class MyLocationService extends Service implements Handler.Callback{
24 31
 
25 32
     private AMapLocationClient locationClient = null;
26 33
     private AMapLocationClientOption locationOption = new AMapLocationClientOption();
27 34
 
28 35
     public static final int COMMAND_START_LOCATION = 9801;
29
-    public static final int COMMAND_STOP_LOCATION = 9802;
30 36
     public static final int COMMAND_DESTROY_LOCATION = 9803;
31 37
 
32 38
     private double lastLat;
33 39
     private double lastLon;
34 40
     private double repeatTime;
35 41
 
36
-    private int status = 0; // 0:未开始  1:已开始 2:已暂停
42
+    private static final int FETCH_GATHER_INFO_PERIOD = 15*60*1000;
43
+    private static final int LOCATION_MAX_PERIOD = 10*60*1000;
44
+    private static final int LOCATION_MIN_PERIOD = 60*1000;
45
+    private static final int LOCATION_PERIOD_CHANGE_DEADLINE = 15*60*1000;
46
+    private static final int MSG_FETCH_TOUR_INFO = 4567;
47
+    private static final int MSG_START_LOCATION = 4568;
48
+
49
+    private Handler mHandler;
50
+
51
+    private HttpPostTask fetchTourInfoTask;
52
+
53
+    @Override
54
+    public void onCreate() {
55
+        super.onCreate();
56
+        mHandler = new Handler(this);
57
+    }
37 58
 
38 59
     @Override
39 60
     public IBinder onBind(Intent intent) {
@@ -51,13 +72,11 @@ public class MyLocationService extends Service {
51 72
         }
52 73
         int command = bundle.getInt("command",0);
53 74
         initLocation();
75
+        initGatherInfo();
54 76
         switch (command) {
55 77
             case COMMAND_START_LOCATION:
56 78
                 startLocation();
57 79
                 break;
58
-            case COMMAND_STOP_LOCATION:
59
-                stopLocation();
60
-                break;
61 80
             case COMMAND_DESTROY_LOCATION:
62 81
                 destroyLocation();
63 82
                 break;
@@ -76,24 +95,93 @@ public class MyLocationService extends Service {
76 95
         if(locationClient!=null){
77 96
             return;
78 97
         }
79
-        //初始化client
80
-        locationClient = new AMapLocationClient(this.getApplicationContext());
81
-        //设置定位参数
98
+        locationClient = new AMapLocationClient(getApplicationContext());
82 99
         locationClient.setLocationOption(getDefaultOption());
83
-        // 设置定位监听
84 100
         locationClient.setLocationListener(locationListener);
85
-        status = 0;
86 101
     }
87 102
 
103
+    private void initGatherInfo(){
104
+        if(!Preferences.getInstance(this).isTourMode()&&Preferences.getInstance(this).getTourEndTime()<System.currentTimeMillis()){
105
+            return;
106
+        }
107
+        long gatherTime = Preferences.getInstance(this).getGatherTime();
108
+        if(gatherTime<System.currentTimeMillis()){
109
+            mHandler.sendEmptyMessageDelayed(MSG_FETCH_TOUR_INFO,60000);
110
+        }
111
+    }
112
+
113
+    private void fetchTourInfo(){
114
+        mHandler.sendEmptyMessageDelayed(MSG_FETCH_TOUR_INFO,FETCH_GATHER_INFO_PERIOD);
115
+        if(!Preferences.getInstance(this).isTourMode()&&Preferences.getInstance(this).getTourEndTime()<System.currentTimeMillis()){
116
+            return;
117
+        }
118
+        long gatherTime = Preferences.getInstance(this).getGatherTime();
119
+        if(gatherTime>System.currentTimeMillis()){
120
+            return;
121
+        }
122
+        if(fetchTourInfoTask!=null && fetchTourInfoTask.getStatus()== AsyncTask.Status.RUNNING){
123
+            return;
124
+        }
125
+        HashMap<String,String> params = new HashMap<String,String>();
126
+        String userId = Preferences.getInstance(this).getUserId();
127
+        if(userId.length()==0){
128
+            return;
129
+        }
130
+        params.put("user_id",userId);
131
+
132
+        fetchTourInfoTask = new HttpPostTask(this,params) {
133
+
134
+
135
+            @Override
136
+            protected boolean parseResponse(Context context, String response) {
137
+                try{
138
+                    JSONObject json = new JSONObject(response);
139
+                    int status = json.getInt("status");
140
+                    if(status == 200){
141
+                        JSONObject data = json.getJSONObject("data");
142
+                        String groupId = data.getString("group_id");
143
+                        if(!TextUtils.isEmpty(groupId)){
144
+                            Preferences.getInstance(context).setTourMode(true);
145
+                            JSONObject group = data.getJSONObject("group");
146
+                            String phone = group.getString("phone");
147
+                            Preferences.getInstance(context).setTourGuidePhone(phone);
148
+                            String gatherTimeStr = group.getString("gather_at");
149
+                            Preferences.getInstance(context).setGatherTime(TimeUtils.getStandardTimeInMiliSec(gatherTimeStr));
150
+                            String endTimeStr = group.getString("ended_at");
151
+                            Preferences.getInstance(context).setTourEndTime(TimeUtils.getStandardTimeInMiliSec(endTimeStr));
152
+                        }else{
153
+                            Preferences.getInstance(context).setTourMode(false);
154
+                            Preferences.getInstance(context).setTourGuidePhone("");
155
+                            Preferences.getInstance(context).setGatherTime(0);
156
+                            Preferences.getInstance(context).setTourEndTime(0);
157
+                        }
158
+                        return true;
159
+                    }
160
+                }catch (Exception e){
161
+                    e.printStackTrace();
162
+                }
163
+                return false;
164
+            }
88 165
 
166
+            @Override
167
+            protected void onPostSuccess(Context context) {
168
+                super.onPostSuccess(context);
169
+                if(mHandler!=null){
170
+                    mHandler.sendEmptyMessage(MSG_START_LOCATION);
171
+                }
172
+            }
173
+        };
174
+        fetchTourInfoTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.TOUR_INFO_URL);
175
+
176
+    }
89 177
     private AMapLocationClientOption getDefaultOption(){
90 178
         AMapLocationClientOption mOption = new AMapLocationClientOption();
91 179
         mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
92 180
         mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
93 181
         mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
94 182
         mOption.setInterval(30*1000);//可选,设置定位间隔。默认为30秒
95
-        mOption.setNeedAddress(false);//可选,设置是否返回逆地理地址信息。默认是true
96
-        mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
183
+        mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
184
+        mOption.setOnceLocation(true);//可选,设置是否单次定位。默认是false
97 185
         mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
98 186
         AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
99 187
         mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
@@ -106,42 +194,39 @@ public class MyLocationService extends Service {
106 194
         public void onLocationChanged(AMapLocation loc) {
107 195
             double lat = loc.getLatitude();
108 196
             double lon = loc.getLongitude();
197
+            LogHelper.dfile("czy","经纬度 lat = "+lat+"lon="+lon+" 位置 ="+loc.getAddress());
109 198
             reportLocationWhenTourMode(lat,lon);
110 199
         }
111 200
     };
112 201
 
113 202
 
114 203
     private void startLocation(){
115
-        if(status==1){
116
-            return;
204
+        initLocation();
205
+        if(Preferences.getInstance(this).isTourMode()){
206
+            locationClient.setLocationOption(locationOption);
207
+            locationClient.startLocation();
208
+            if(Preferences.getInstance(this).getGatherTime()> System.currentTimeMillis()){
209
+                mHandler.sendEmptyMessageDelayed(MSG_START_LOCATION,Preferences.getInstance(this)
210
+                        .getGatherTime()-System.currentTimeMillis()>LOCATION_PERIOD_CHANGE_DEADLINE ? LOCATION_MAX_PERIOD:LOCATION_MIN_PERIOD);
211
+            }
117 212
         }
118
-        // 设置定位参数
119
-        locationClient.setLocationOption(locationOption);
120
-        // 启动定位
121
-        locationClient.startLocation();
122
-
123
-        status = 1;
124 213
     }
125 214
 
126
-    private void stopLocation(){
127
-        if(status == 2){
128
-            return;
129
-        }
130
-        // 停止定位
131
-        locationClient.stopLocation();
132
-        status = 2;
133
-    }
134 215
 
135 216
     private void destroyLocation(){
136 217
         if (null != locationClient) {
218
+            locationClient.stopLocation();
137 219
             locationClient.onDestroy();
138 220
             locationClient = null;
139 221
             locationOption = null;
140
-            status = 0;
141 222
         }
223
+        mHandler.removeCallbacksAndMessages(null);
142 224
     }
143 225
 
144 226
 private void reportLocationWhenTourMode(double lat, double lon){
227
+    if(locationClient!=null){
228
+        locationClient.stopLocation();
229
+    }
145 230
     if(lat == lastLat && lon ==lastLon && repeatTime<5){
146 231
         repeatTime++;
147 232
         return;
@@ -149,18 +234,26 @@ private void reportLocationWhenTourMode(double lat, double lon){
149 234
     repeatTime = 0;
150 235
     lastLat = lat;
151 236
     lastLon = lon;
152
-    if(Preferences.getInstance(this).isTourMode()){
153
-        HashMap<String,String> params = new HashMap<String,String>();
154
-        String userId = Preferences.getInstance(this).getUserId();
155
-        if(userId.length()==0){
156
-            return;
157
-        }
158
-        params.put("user_id",userId);
159
-        params.put("lon",String.valueOf(lon));
160
-        params.put("lat",String.valueOf(lat));
161
-        new HttpPostTask(this,params).executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.GEO_LOCATION_URL);
162
-        LogHelper.dfile("czy","上报经纬度位置 lat = "+lat+"lon="+lon);
237
+    HashMap<String,String> params = new HashMap<String,String>();
238
+    String userId = Preferences.getInstance(this).getUserId();
239
+    if(userId.length()==0){
240
+        return;
163 241
     }
242
+    params.put("user_id",userId);
243
+    params.put("lon",String.valueOf(lon));
244
+    params.put("lat",String.valueOf(lat));
245
+    new HttpPostTask(this,params).executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.GEO_LOCATION_URL);
164 246
 }
165 247
 
248
+    @Override
249
+    public boolean handleMessage(Message msg) {
250
+        if(msg.what == MSG_FETCH_TOUR_INFO){
251
+            fetchTourInfo();
252
+            return true;
253
+        }else if(msg.what == MSG_START_LOCATION){
254
+            startLocation();
255
+            return true;
256
+        }
257
+        return false;
258
+    }
166 259
 }